package com.bootdo.system.controller;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.system.domain.MenuDO;
import com.bootdo.system.service.MenuService;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@RequestMapping("/sys/menu")
@Controller
@Api(value = "/sys/menu", description = "菜单接口")
public class MenuController extends BaseController {
	
	@Autowired
	MenuService menuService;


	@GetMapping("/list")
	@ResponseBody
	@ApiOperation(value="菜单查询", notes="", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "menuId", value = "菜单id", required = false,paramType="query", dataType = "Long"),
        @ApiImplicitParam(name = "pageSize", value = "每页条数", required = true,paramType="query", dataType = "Integer"),
        @ApiImplicitParam(name = "pageNum", value = "当前页数", required = true,paramType="query", dataType = "Integer")
	})
	R list(@RequestParam Map<String, Object> params)  throws Exception{
		 	Query query = new Query(params);
			List<MenuDO> address = menuService.list(query);
			PageInfo<MenuDO> pageInfo = new PageInfo<MenuDO>(address);
			int total = (int)pageInfo.getTotal();
			PageUtils pageUtil = new PageUtils(address, total);
			return new R("list",pageUtil);
	}


	@ApiOperation(value="获取菜单详情", notes="", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "菜单id", required = true,paramType="query", dataType = "Long"),
	})
	@GetMapping("/edit")
	@ResponseBody
	R edit(Long id)  throws Exception{
		return new R("obj", menuService.get(id));
	}

	@ApiOperation(value="保存", notes="", response=String.class)
	@PostMapping("/save")
	@ResponseBody
	R save(@RequestBody MenuDO menu)  throws Exception{
		if (menuService.save(menu) > 0) {
			return R.saveOk(menu.getMenuId()+"");
		} else {
			return R.error(1, "保存失败");
		}
	}

	@ApiOperation(value="更新", notes="", response=String.class)
	@PostMapping("/update")
	@ResponseBody
	R update(@RequestBody MenuDO menu)  throws Exception{
		if (menuService.update(menu) > 0) {
			return R.ok();
		} else {
			return R.error(1, "更新失败");
		}
	}

	@ApiOperation(value="菜单删除", notes="", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "菜单id", required = true,paramType="query", dataType = "Long"),
	})
	@GetMapping("/remove")
	@ResponseBody
	R remove(Long id)  throws Exception{
		if (menuService.remove(id) > 0) {
			return R.ok();
		} else {
			return R.error(1, "删除失败");
		}
	}

	@GetMapping("/tree")
	@ResponseBody
	R tree()  throws Exception{
		Tree<MenuDO> tree = new Tree<MenuDO>();
		tree = menuService.getTree();
		return new R("obj",tree);
	}

	@GetMapping("/treeByRoleId")
	@ResponseBody
	@ApiOperation(value="获取菜单详情", notes="", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "roleId", value = "角色id", required = true,paramType="query", dataType = "Long"),
	})
	R tree(Long roleId)  throws Exception{
		Tree<MenuDO> tree = new Tree<MenuDO>();
		tree = menuService.getTree(roleId);
		return new R("obj",tree);
	}
	
	@GetMapping("/treeByUserId")
	@ResponseBody
	@ApiOperation(value="获取用户菜单", notes="", response=String.class)
	@ApiImplicitParams({
        @ApiImplicitParam(name = "userId", value = "用户id", required = true,paramType="query", dataType = "Long"),
	})
	R treeByUserId(Long userId)  throws Exception{
		List<Tree<MenuDO>> tree = menuService.listMenuTree(userId);
		return new R("list",tree);
	}
	
}
